<?php
/**
 * 售后
 */
$up_url="../";
include (dirname(__FILE__)."/session.inc.php");//全局变量

//页面属于
$mod=trim(empty($_REQUEST['mod'])?"list":$_REQUEST['mod']);
$smarty->assign("mod",$mod);
$link_url="";
$tb_service="`".$tbprefix."service`";
/*分页数*/
$pagesize =10;
//当前页
$page =empty($_REQUEST['page'])||($_REQUEST['page']<0)?"1":trim($_REQUEST['page']);

switch ($mod){
	//信息编辑
	case "edit":

		//页面动作
		@$act=isset($_REQUEST['act'])?trim($_REQUEST['act']):"";
		@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		/**
		 * 获取地址栏传递的id
		 */
		$pid=empty($_GET['pid'])?"0":$_GET['pid'];
		$smarty->assign("pid",$pid);
		
		$province = $db->get_all ( "SELECT infoid, title FROM `{$tbprefix}area` WHERE parentid='0' AND `level`='1' AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province", $province );
		
		switch ($act){
			//添加
			case "add":
			
				$titles_edit="添加售后服务点信息";
				$oneinfo=array("id"=>0);
			
				break;
			//更新
			case "update":

				$titles_edit="修改售后服务点信息";
				//商户信息
				$oneinfo=$db->get_one("SELECT `id`, `merchant_name`, `merchant_no`, `province`, `city`, `area`, `address`, `tel`, `mobile`, `qq`, `e_mail`, `contacts`, `is_show`, `lat`, `lng`,`is_valid`, `addtime`, `change_time`, `del_time`, `content`,`listorder` FROM ".$tb_service." WHERE  `id`='".$pid."' AND `is_valid`='1' AND 1");
				if(empty($oneinfo['id'])){
					$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
					break;
				}
				
				if ($oneinfo['province']) {
					$sql_city = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `parentid`='".$oneinfo['province']."' AND `level`='2' AND `ishow`='1' AND 1 ORDER BY `orderid` DESC";
					$cityList = $db->get_all ( $sql_city );
					$smarty->assign ( "cityList", $cityList ); //市
				}
				if ($oneinfo['city']) {
					$sql_area = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `parentid`='".$oneinfo['city']."' AND `level`='3' AND `ishow`='1' AND 1 ORDER BY `orderid` DESC";
					$areaList = $db->get_all ( $sql_area );
					$smarty->assign ( "areaList", $areaList ); //市
				}
				$oneinfo['address']=empty($oneinfo['address'])?"":$fun->showInput($oneinfo['address']);
				break;

				//修改
			case "change":
				$hid=isset($_POST['actId'])?$_POST['actId']:"";
				$merchant_name_input=$fun->checkInput(trim($_POST['merchant_name']?$_POST['merchant_name']:""));
				$dataArray['merchant_name']=$merchant_name_input;
				
				$merchant_name_top=$fun->top_pinyin($merchant_name_input,1)?$fun->top_pinyin($merchant_name_input,1):"X";
				$merchant_area=trim(!empty($_POST['area'])?$_POST['area']:"0");
				$dataArray['merchant_no']=$merchant_area."-".$merchant_name_top;
	
				$dataArray['province']=trim(!empty($_POST['province'])?$_POST['province']:"0");
				$dataArray['city']=trim(!empty($_POST['city'])?$_POST['city']:"0");
				$dataArray['area']=trim(!empty($_POST['area'])?$_POST['area']:"0");
				$dataArray['address']=$fun->checkInput(trim($_POST['address']?$_POST['address']:""));
				$dataArray['tel']=$fun->checkInput(trim($_POST['tel']?$_POST['tel']:""));
				$dataArray['mobile']=$fun->checkInput(trim($_POST['mobile']?$_POST['mobile']:""));
				$dataArray['qq']=$fun->checkInput(trim($_POST['qq']?$_POST['qq']:""));
				$dataArray['e_mail']=$fun->checkInput(trim($_POST['e_mail']?$_POST['e_mail']:""));
				$dataArray['contacts']=$fun->checkInput(trim($_POST['contacts']?$_POST['contacts']:""));
				$dataArray['is_show']=(trim(!empty($_POST['is_show'])?$_POST['is_show']:"0"));
				$dataArray['lat']=(trim(isset($_POST['lat'])?$_POST['lat']:""));
				$dataArray['lng']=(trim(isset($_POST['lng'])?$_POST['lng']:""));
				$dataArray['content']=$fun->checkInput(trim($_POST['content']?$_POST['content']:""));
				$dataArray['listorder']=$fun->checkInput(trim(isset($_POST['listorder'])?$_POST['listorder']:"999"));
				

				if($hid){
					//更新
					$dataArray['change_time']=$m_now_time;//修改数据时间
					if($db->update($tb_service,$dataArray,$condition="`id`='".$hid."' AND 1"))
					{
						$fun->msg('更新成功','?mod=edit&act=update&pid='.$hid.'&page='.$page,1);
						$db->close();
					}
					else
					{
						$fun->msg('更新失败','?mod=edit&act=update&pid='.$hid.'&page='.$page,1);
					}
				}else{
					//添加
					$dataArray['addtime']=$m_now_time;
					$dataArray['is_valid']=1;//有用数据
					if($db->insert($tb_service,$dataArray))
					{
						$fun->msg('添加成功','?mod=edit&act=add&page='.$page,1);
						$db->close();
					}
					else
					{
						$fun->msg('添加失败','?mod=edit&act=add&page='.$page,1);
					}
				}
				break;

			default:
				$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
				break;
		}
		
		$smarty->assign("one",$oneinfo);//要修改的信息
		$smarty->assign("titleAct",$titles_edit);//页面操作动作标题
		$smarty->assign("Returnfpage",$back_url);//返回列表

		break;
	
	case "delone"://单个删除
			@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		
			$id=!empty($_REQUEST['delete_id'])?$fun->checkInput($_REQUEST['delete_id']):"";
			$users_del_info=$db->get_one("SELECT `id` FROM `{$tbprefix}service` WHERE `id`='{$id}' AND `is_valid`='1' AND 1");
			/*单个删除*/
			if($users_del_info['id']){
				$dataArray['del_time']=$m_now_time;
				$dataArray['del_cause']=!empty($_REQUEST['del_cause'])?$fun->checkInput($_REQUEST['del_cause']):"";
				$dataArray['is_valid']="0";
				$del_cata=$db->update($table=" `{$tbprefix}service` ",$dataArray,$condition="`id`='".$id."' AND 1");
				if($del_cata==true){
					//记录操作
					$fun->msg("删除成功",$back_url,1);
				}
				else{
					$fun->msg("删除失败",$back_url,1);
				}
			}else{
				$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
			}
		
			break;
			
	//是否显示
	case "ishow":
		/*更新是否显示*/
		@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		if($adminPower<1){
			$id=empty($_GET['id'])?0:$_GET['id'];
			$user_total=$db->get_one("SELECT COUNT(`id`) AS `total` FROM `{$tbprefix}service` WHERE `id`='$id' AND `is_valid`='1' AND 1");
			switch ($user_total['total']){
				case 0:
					//管理员权限
					$fun->msg("无效的售后点,正在跳转...",$back_url,1);
					break;
				default:
					$ishow=isset($_GET['ishowId'])?$_GET['ishowId']:($fun->msg("操作失败,正在跳转...",$back_url,1));
					$db->query("UPDATE `{$tbprefix}service` SET `is_show`='".$ishow."' WHERE `id`='".$id."' AND 1");
					echo "<script>window.location.href='$back_url';</script>";
					break;
			}
			}else{
			$fun->msg("权限不足",$back_url,1);//错误跳转到新的页面
			}
			break;

		//信息列表
	default:

		$condition="";

		$province_get=trim(empty($_GET['province'])?"0":$fun->checkInput($_GET['province']));
		$city_get=trim(empty($_GET['city'])?"0":$fun->checkInput($_GET['city']));
		$keyword=trim(empty($_GET['keyword'])?"":$fun->checkInput($_GET['keyword']));
		$mobile=trim(empty($_GET['mobile'])?"":$fun->checkInput($_GET['mobile']));
		$is_show=trim(empty($_GET['is_show'])?"0":$_GET['is_show']);
		$t1=trim(empty($_GET['t1'])?"":$_GET['t1']);
		$t2=trim(empty($_GET['t2'])?"":$_GET['t2']);
		if($keyword){
			$keywords = str_replace(" ","%",trim(urldecode($keyword)));
			$condition.="((s.`merchant_name` LIKE '%".$keywords."%')OR(s.`contacts` LIKE '%".$keywords."%')) AND ";
		}
		$smarty->assign("keyword",$fun->showInput($keyword, ENT_QUOTES, "UTF-8" ));
		
		if($mobile){
			$mobiles =  str_replace(" ","%",trim(urldecode($mobile)));
			$condition.="((s.`mobile` LIKE '%".$mobiles."%')) AND ";
		}
		$smarty->assign("mobile",$fun->showInput($mobile, ENT_QUOTES, "UTF-8" ));
		
		if($t1){
			$ts1=strtotime($t1);
			$condition.="(s.`addtime`>'".$ts1."') AND ";
		}
		$smarty->assign("t1",$t1);
		if($t2){
			$ts2=strtotime($t2." 23:59:59");
			$condition.="(s.`addtime`<'".$ts2."') AND ";
		}
		$smarty->assign("t2",$t2);
		
		if($is_show>0){
			$is_shows = ($is_show-1>0)?1:0;
			$condition.="(s.`is_show`='".$is_shows."') AND ";
		}
		$smarty->assign("is_show",$is_show);
		
		// 查询区域
		$province = $db->get_all ( "SELECT infoid, title FROM `{$tbprefix}area` WHERE parentid='0' AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC" );
		$smarty->assign ( "province", $province );
		if ($province_get) {
			$sql_city = "SELECT infoid, title FROM `{$tbprefix}area` WHERE `parentid`='".$province_get."' AND `is_valid`='1' AND 1 ORDER BY `orderid` DESC";
			$cityList = $db->get_all ( $sql_city );
			$smarty->assign ( "cityList", $cityList ); // 市
			$condition .= " (s.`province` ='".$province_get."') AND ";
		}
		if ($city_get) {
			$condition .= " (s.`city`='".$city_get."') AND ";
		}
		$smarty->assign("province_get",$province_get);
		$smarty->assign("city_get",$city_get);
		
		$fpage = $fun->setPageUri();//该页面;

		$mer_sql="SELECT s.`id`, s.`merchant_name`, s.`merchant_no`, s.`province`, s.`city`, s.`area`, 
		s.`tel`, s.`mobile`, s.`qq`, s.`contacts`, s.`is_show`, s.`e_mail`,s.`lat`,s.`lng`,
		s.`is_valid`, s.`addtime`, s.`change_time`, s.`del_time`,s.`listorder`,a1.`title` AS `province_value`,
		a2.`title` AS `city_value`,a3.`title` AS `area_value`  
		FROM `{$tbprefix}service` AS s
		LEFT JOIN `{$tbprefix}area` AS a1 ON s.province=a1.infoid
		LEFT JOIN `{$tbprefix}area` AS a2 ON s.city=a2.infoid
		LEFT JOIN `{$tbprefix}area` AS a3 ON s.area=a3.infoid
		WHERE {$condition} s.`is_valid`='1' AND 1 ORDER BY s.`listorder` DESC,s.`addtime` DESC";

		/*--分页开始--*/
		$numpage = $db->pagedeal($mer_sql, $pagesize);//调用分页处理函数

		//SQL LIMIT起始
		$startloc=(($page-1)*$pagesize<0)?0:($page-1)*$pagesize;

		$merValue=$db->get_all($mer_sql." LIMIT $startloc,$pagesize");
		
		$i=0;											//定义序号初始为0
		$merchant_arr=array();
		foreach ($merValue as $key=>$value){
			//序号
			$i++;$pageid=(($page-1)*$pagesize)+$i;
			$merchant_arr[$key]=$value;
			//时间
			$addtimeDate=!empty($value['addtime'])?date("Y-m-d H:i:s",$value['addtime']):"0000-00-00 00:00:00";
			if($t1 || $t2){
				$addtimeDate=str_ireplace($addtimeDate,"<span class=\"xinghao\">".$addtimeDate."</span>",$addtimeDate);
			}
			//==查询描红加粗==//
			$merchant_arr[$key]['pageid']=$pageid;
			$merchant_arr[$key]['province_value']=!empty($value['province_value'])?$fun->showInput($value['province_value']):"";
			$merchant_arr[$key]['city_value']=!empty($value['city_value'])?$fun->showInput($value['city_value']):"";
			$merchant_arr[$key]['area_value']=!empty($value['area_value'])?$fun->showInput($value['area_value']):"";
			$merchant_arr[$key]['merchant_name']=!empty($value['merchant_name'])?$fun->showInput($value['merchant_name']):"";
			$merchant_arr[$key]['addtime']=$addtimeDate;
			$merchant_arr[$key]['change_time']=!empty($value['change_time'])?date("Y-m-d H:i:s",$value['change_time']):"0000-00-00 00:00:00";
			$merchant_arr[$key]['set_latlng']=(empty($value['lat'])||empty($value['lng']))?0:(($value['lat']>0.0000000001||$value['lng']>0.0000000001)?1:0);
			
		}//print_r($merchant_arr);
		$smarty->assign("merchant_arr",$merchant_arr);			
		$act_url=$_SESSION['act_url']=$fun->GetCurUrl(1);//获取URL的路径存入session
		//----------------------分页相关的模板变量替换-------------------------//
		$smarty->assign("recordcount", $recordcount); 	//总记录数
		$smarty->assign("pagecount", $pagecount);  		//总页数
		$smarty->assign("prepage", $page - 1);     		//上一页
		$smarty->assign("nexpage", $page + 1);     		//下一页
		$smarty->assign("numpage", $numpage);    		//数字分页
		$smarty->assign("fpage", $fpage); 		//要分页的网页
		
		/*批量更新排序*/
		if(!empty($_POST['replace'])){
		$all_id=isset($_POST['all'])?$_POST['all']:"";//所有id
		if($adminPower<1){
			if(isset($all_id)){
				$order_id=isset($_POST['rep'])?$_POST['rep']:"";//更新的排序id
				foreach($all_id as $key=>$value){
					$db->query("UPDATE `{$tbprefix}service` SET `listorder`='".$order_id[$key]."' WHERE `id`='".$value."' AND 1");
				}
				echo "<script>window.location.href='$act_url';</script>";
			}
			}else{
			$fun->msg("权限不足",$act_url,1);//错误跳转到新的页面
			}
		}

	break;
}

$smarty->assign("page",$page);
$smarty->assign("title",$cfg['sysName']);
@$smarty->display("service.tpl");
?>